package com.ellecity06.cwhiskymall.ui.mine.auth.result

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.common.widgets.dialog.showAlert
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.bean.response.MemberInfo
import com.ellecity06.cwhiskymall.databinding.ActivityAuthInfoBinding
import com.ellecity06.cwhiskymall.ui.webview.WebHelper


/**
 * @author ellecity06
 * @time 2024/10/26 11:53
 * @des 个人中心页面
 */
class AuthInfoActivity : BaseMvvmActivity<ActivityAuthInfoBinding, AuthInfoViewModel>(),
    View.OnClickListener {
    private var visibleInfo = false
    override fun inflateBinding(): ActivityAuthInfoBinding {
        return ActivityAuthInfoBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        title = "实名认证中心"
        initView()
        fetchUser()


    }

    private var userInfo: MemberInfo? = null
    override fun initObserver() {
        viewModel.state.observeEvent(this) {
            when (it) {
                is AuthInfoState.UserInfo -> {
                    userInfo = it.user
                    setUserInfo(it.user)
                }

                else -> {}
            }
        }
    }

    private fun setUserInfo(user: MemberInfo) {
        setEyeIcon()
        if (visibleInfo) {
            binding.tvName.setText(user.realName ?: "暂无姓名")
            binding.tvPhone.text = user.idCard
        } else {
            binding.tvName.setText(hideLastName(user.realName ?: ""))
            binding.tvPhone.text = hideIdNumber(user.idCard ?: "")
        }
    }

    private fun hideIdNumber(idNumber: String): String {
        // 正则表达式：匹配第一个字符和最后一个字符，中间部分替换为 *
        val regex = Regex("^(.).*(.)$")

        // 替换中间部分为 *
        return regex.replace(idNumber, "$1${"*".repeat(idNumber.length - 2)}$2")
    }

    private fun hideLastName(name: String): String {
        // 正则表达式：匹配最后一个字符（汉字、字母或其他字符）
        val regex = Regex("(.*)(.)$")

        // 替换最后一个字符为 *
        return regex.replace(name, "$1*")
    }

    private fun setEyeIcon() {
        binding.ivAvatar.setImageResource(if (visibleInfo) R.mipmap.icon_theme_eye else R.mipmap.icon_theme_noeye)
    }

    private fun fetchUser() {
        lifecycleScope.launchWhenCreated {
            viewModel.intentChannel.send(AuthInfoIntent.User)
        }
    }

    private fun initView() {
        setRightClickListener(this)
        binding.tvUpdate.setOnClickListener(this)
        binding.tvBiangeng.setOnClickListener(this)
        binding.ivAvatar.setOnClickListener(this)
        binding.ivAgreement.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        when (view.id) {
            binding.ivAgreement.id -> {
                WebHelper.toPrivateAgreement(this)
            }

            binding.tvBiangeng.id,
            binding.tvUpdate.id -> {
                showAlert(
                    title = "变更认证",
                    content = "为保护您的个人隐私，再次更新或变更实名认证，需联系客服在线处理，客服会引导您接下来的操作，给您带来的不便敬请 谅解。",
                    cancelText = "我知道了",
                    confirmText = "联系客服"
                ) {
                    WebHelper.toKefu(this)
                }
            }

            binding.ivAvatar.id -> {
                userInfo?.let {
                    visibleInfo = !visibleInfo
                    setUserInfo(it)
                }

            }


        }
    }


}